﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KxParser.Parser.LR0;

namespace KxParser.Parser.LR
{
    class ParserTableEx
    {

        private readonly StateEx[] _states;
        //private object[,] _table;// = new string[row, column];
        private DataTable _dataTable;

        public ParserTableEx(StateEx[] states)
        {
            _states = states;

            InitParserTable();

            FillParserTable();

            KxParserGlobal.TransitionTable = _dataTable;
        }

        private void InitParserTable()
        {
            _dataTable = new DataTable("parsertable");

            // init columns
            _dataTable.Columns.Add("StateId", typeof (int));
            foreach (string terminal in KxParserGlobal.Terminals)
            {
                _dataTable.Columns.Add(terminal, typeof (TableAction));
            }
            foreach (string nonTerminal in KxParserGlobal.NonTerminals)
            {
                _dataTable.Columns.Add(nonTerminal, typeof(TableAction));
            }

            // init rows
            foreach (StateEx state in _states)
            {
                List<object> lst = new List<object>();
                lst.Add(state.Id);
                for (int i=1; i<_dataTable.Columns.Count;i++)
                {
                    lst.Add(null);
                }
                
                DataRow row = _dataTable.Rows.Add();
                row.ItemArray = lst.ToArray();
            }
        }

        private void FillParserTable()
        {
            try
            {
                var tblActions = new List<TableAction>();

                foreach (var state in _states)
                {
                    int row = GetRow(state.Id);

                    foreach (var productionItem in state.ProductionItems)
                    {
                        TableAction action = GetAction(productionItem);
                        int col = GetCol(productionItem.OverItem);

                        if (col != -1)
                            _dataTable.Rows[row][productionItem.OverItem] = action;
                    }
                }
            }catch(Exception ex)
            {
                KxParserGlobal.InternalExceptions.Add(ex);
            }
        }

        private TableAction GetAction(ProductionItemEx productionItem)
        {
            try
            {
                if (productionItem.IsEnder)
                {
                    return new TableAction {Action = TableAction.EAction.Accept};
                }

                if (productionItem.CurrentState.Id > 1 && productionItem.ContainsRule != -1
                    /*&& productionItem.CurrentState.GetProductionItems().Length==1*/)
                {
                    return new TableAction {Action = TableAction.EAction.Reduce, State = productionItem.ContainsRule};
                }

                if (productionItem.NextState == null)
                {
                    return new TableAction {Action = TableAction.EAction.Error};
                }

                if (KxParserGlobal.Terminals.Contains(productionItem.OverItem))
                {
                    return new TableAction {Action = TableAction.EAction.Shift, State = productionItem.NextState.Id};
                }

                if (KxParserGlobal.NonTerminals.Contains(productionItem.OverItem))
                {
                    return new TableAction {Action = TableAction.EAction.Goto, State = productionItem.NextState.Id};
                }

                //return new TableAction {Action = TableAction.EAction.Error};
            }catch(Exception ex)
            {
                KxParserGlobal.InternalExceptions.Add(ex);
            }
            return new TableAction { Action = TableAction.EAction.Error };
        }

        public TableAction GetAction(int stateId, string terminal)
        {
            var o = _dataTable.Rows[GetRow(stateId)][terminal];
            if (o == null)
                return null;

            if(typeof(TableAction).IsAssignableFrom(o.GetType()))
                return (TableAction)o;
            return null;
        }

        int GetRow(int stateId)
        {
            for(int i=0; i<_states.Length; i++)
            {
                if(_states[i].Id == stateId)
                {
                    return i;
                }
            }
            return 0;
        }

        int GetCol(string columnName)
        {
            for (int i = 0; i < _dataTable.Columns.Count; i++)
            {
                if (_dataTable.Columns[i].ColumnName.Equals(columnName))
                {
                    return i;
                }
            }
            return -1;
        }
    }

    //internal class TableActionz
    //{
    //    private List<TableAction> actionzList = new List<TableAction>();

    //    public void Add(TableAction tableAction)
    //    {
    //        actionzList.Add(tableAction);
    //    }

    //    public TableAction[] GetActions()
    //    {
    //        return actionzList.ToArray();
    //    }
    //}
}
